package com.study.springCustom.spring.reader;

import com.study.springCustom.spring.registry.BeanDefinitionRegistry;
import com.study.springCustom.spring.utils.DocumentUtils;
import org.dom4j.Document;

import java.io.InputStream;

// 专门对XML进行解析，获取Document，最终取解析Document完成BeanDefinition的注册
public class XmlBeanDefinitionReader implements BeanDefinitionReader {

    private BeanDefinitionRegistry registry;

    public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) {
        this.registry = registry;
    }

    @Override
    public void registerBeanDefinitions(InputStream inputStream) {
        // 创建Document
        Document document = DocumentUtils.getDocument(inputStream);
        // 解析Document，获取BeanDefinition
        BeanDefinitionDocumentReader documentReader = new DefaultBeanDefinitionDocumentReader(registry);
        // 按照Spring语义解析Document
        documentReader.loadBeanDefinitions(document.getRootElement());
    }


}
